package com.hanyizhu.demostore.service.impl;

import com.hanyizhu.demostore.entity.Cart;
import com.hanyizhu.demostore.mapper.CatrMapper;
import com.hanyizhu.demostore.mapper.ProductMapper;
import com.hanyizhu.demostore.service.ICartService;
import com.hanyizhu.demostore.service.ex.AccessDeniedException;
import com.hanyizhu.demostore.service.ex.CartNotFoundException;
import com.hanyizhu.demostore.service.ex.InserException;
import com.hanyizhu.demostore.service.ex.UpdateException;
import com.hanyizhu.demostore.vo.CartVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author lenovo
 */
@Service
public class CartServiceImpl implements ICartService {

    @Autowired
    private CatrMapper catrMapper;
    @Autowired
    private ProductMapper productMapper;
    @Override
    public void addToCart(Integer uid, Integer pid, Integer amount, String username) {
        Date date = new Date();
        //当前要添加的商品是否在表中以及存在
        Cart result = catrMapper.findByUidAndPid(uid, pid);
        if (result == null){//表示商品从来没有添加到购物车中,则进行插入操作
            //需要创建一个cart对象
            Cart cart = new Cart();
            cart.setUid(uid);
            cart.setPid(pid);
            cart.setNum(amount);
            //价格来自商品的数据
            Long price = productMapper.findById(pid).getPrice();
            cart.setPrice(price);
            //补全日志
            cart.setCreatedUser(username);
            cart.setCreatedTime(date);
            cart.setModifiedTime(date);
            cart.setModifiedUser(username);
            //添加购物车
            Integer rows = catrMapper.insert(cart);
            if (rows !=1){
                throw new InserException("插入数据时产生未知的异常");
            }
        }
        if (result != null){//表示当前商品在购物车已经存在则进行更新
            Integer num = result.getNum() + amount;
            Integer rows = catrMapper.updateNumByCid(result.getCid(), num, username, date);
            if (rows != 1){
                throw new UpdateException("更新数据时产生的未知异常");
            }
        }
    }

    @Override
    public List<CartVO> getVoByUid(Integer uid) {
        return catrMapper.findByUid(uid);
    }

    @Override
    public Integer addNum(Integer cid, Integer uid, String username) {
        Cart result = catrMapper.findByCid(cid);
        if (result == null){
            throw new CartNotFoundException("商品不存在!");
        }
        if (!result.getUid().equals(uid)){
            throw new AccessDeniedException("数据非法访问!");
        }
        Integer num = result.getNum() + 1;
        Integer rows = catrMapper.updateNumByCid(cid, num, username, new Date());
        if (rows !=1){
            throw new UpdateException("更新数据失败");
        }
        //返回新的购物车数据的总量
        return num;
    }

    @Override
    public Integer reduceNum(Integer cid, Integer uid, String username) {
        Cart result = catrMapper.findByCid(cid);
        if (result == null){
            throw new CartNotFoundException("商品不存在");
        }
        if (!result.getUid().equals(uid)){
            throw new AccessDeniedException("数据非法访问");
        }
        Integer num = result.getNum() - 1;
        Integer rows = catrMapper.updateNumByCid(cid, num, username, new Date());
        if (rows != 1){
            throw new UpdateException("更新数据失败");
        }
        return num;
    }

    @Override
    public List<CartVO> getVoByCid(Integer uid, Integer... cid) {
        List<CartVO> list = catrMapper.findVOByCid(cid);
        list.removeIf(l -> !l.getUid().equals(uid));
        return list;
    }

}
